<?
session_start();
$LG1 = "<a href=\"";
$LG2 = "admin_logout.php\"";
$LG3 = ">logout</a>\"";
$LG = $LG1.$LG2.$LG3;
$Sign = '';
//if (!isset($_SESSION["sess_Username"])) { 
?>
  <script>
//	alert('กรุณาทำการ Login เข้าระบบก่อน');
	</script>
    
    <?php $LG="";?>
    
	<!--<meta http-equiv='refresh' content='0;URL=admin_login.php'>-->
<?
include "connectdb.php" ;
 //} ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>ยินดีต้อนรับเข้าสู่ ระบบบริการสื่อมัลติมีเดียด้านการเกษตร</title>
<style type="text/css">
<!--
#apDiv1 {
	position:absolute;
	left:270px;
	top:93px;
	width:43px;
	height:30px;
	z-index:1;
}
#apDiv2 {
	position:absolute;
	left:200px;
	top:10px;
	width:830px;
	height:170px;
	z-index:1;
	background-image: url(img/img02.jpg);
}
body {
	background-image: url(img/bg_new.gif);
	background-repeat: repeat;
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
body,td,th {
	color: #FFFFFF;
}
.style7 {color: #372412; font-weight: bold; }
#apDiv3 {
	position:absolute;
	left:240px;
	top:656px;
	width:45px;
	height:17px;
	z-index:1;
}
#apDiv4 {
	position:absolute;
	left:195px;
	top:24px;
	width:902px;
	height:79px;
	z-index:1;
}
#apDiv5 {
	position:absolute;
	left:210px;
	top:173px;
	width:830px;
	height:56px;
	z-index:2;
}
.style7 {color: #372412; font-weight: bold; }
.style16 {color: #372412; font-weight: bold; font-family: "Times New Roman", Times, serif; }
.style19 {font-size: small}
.style22 {
	color: #372412;
	font-weight: bold;
	font-size: small;
}
.style23 {font-size: x-small}
.style24 {color: #372412; font-weight: bold; font-size: x-small; }
.style25 {color: #372412; font-size: x-small; }
.style26 {color: #372412; font-size: x-small; }
.style27 {color: #000000}
.style28 {color: #000000; font-weight: bold; }
-->
</style>
<script>  
	function txtreset()
		{
		document.frmSearch.txtkeyword.value="";
		}
</script>
<?php 
function remember(){

		for ($i=0;$i<count($_GET["cb_array"]);$i++) {
				$cb_array = $cb_array[$i];
			}
			
}
?>
<?php 
function sss(){
$Sign = 'Hello World';
return $Sign;
}
?>
<?php //ฟังก์ชั่น ค้นหาใน XML
function xmldom(){

	$keyword = 'แตง';
	$doc = new DOMDocument();
  	$url='xml/KitchenGardenVegetables.xml';
  	$doc->load($url);
	
	$items = $doc->getElementsByTagName($keyword)->item(0);
	
	$count_syn_of =0;  
	$Target_syn_of = '';

	$count_is_a =0;  
	$Target_is_a = '';
  
	$i=0;
	$Target = '';
	
	$test = $items->childNodes;
	
			foreach($test as $testloop){
			
					if($testloop->nodeType == XML_ELEMENT_NODE) {
				
							/*echo '<br>';
							echo 'nodeType ->'.$testloop->nodeType.'<br>';
							echo 'nodeName -> '.$testloop->nodeName.'<br>';
							echo 'nodeAttType -> '.$testloop->attributes->item(0)->nodeValue.'<br>';
							echo 'nodeValue -> '.$testloop->nodeValue.'<br>';*/
							if ($testloop->attributes->item(0)->nodeValue == ''){
							/*echo 'I Like This Node'.'<br>';*/
							$Target[$i] = $testloop->nodeName;
							/*echo $Target[$i].'<br>';*/
							$i++;
							}
							
							if ($testloop->attributes->item(0)->nodeValue == 'is-a'){
							$Target_is_a[$count_is_a] = $testloop->nodeValue;
							$count_is_a++;
							}
							
							if ($testloop->attributes->item(0)->nodeValue == 'syn-of'){
							$Target_syn_of[$count_syn_of] = $testloop->nodeValue;
							$count_syn_of++;
							}
				
					}
			
		  }
		  
		   for($a=0;$a<$i;$a++){
  			echo 'Target['.$a.'] = ',$Target[$a],'<br>';
			}
			echo '<br>';
		  echo 'is-a = '.$count_is_a;
		  echo '<br>';
		  
			 for($i=0;$i<$count_is_a;$i++){
			echo 'Target_is_a['.$i.'] = ',$Target_is_a[$i],'<br>';
			}
		  echo 'syn-of = '.$count_syn_of;
		  echo '<br>';
		  
			for($i=0;$i<$count_syn_of;$i++){
			echo 'Target_syn_of['.$i.'] = ',$Target_syn_of[$i],'<br>';
			$keyword_syn = $Target_syn_of[$i];
			}
		  
		  
		  
}
?>

<script type="text/javascript">
<!--
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
</script>

</head>

<body onLoad="MM_preloadImages('img/img09_2.jpg')">
<div id="apDiv5">
  <table width="100%" height="45" border="0" cellpadding="0" cellspacing="0">
    <tr>
      <td width="103" height="45" align="center" background="img/img09.jpg"><a href="index.php"><span class="style7 style23">หน้าหลัก</span></a></td>
      <td width="103" height="45" align="center" background="img/img09.jpg"><span class="style7 style23"><a href="vdo_list.php" class="style7">สื่อมัลติมีเดีย</a></span></td>
      <td width="103" height="45" align="center" background="img/img09.jpg"><span class="style7 style23">สืบค้น<br>เชิงความหมาย</span></td>
      <td width="103" height="45" align="center" background="img/img09.jpg"><span class="style7 style23"><a href="user_register.php" class="style7">ลงทะเบียน</a></span></td>
      <td width="103" height="45" align="center" background="img/img09.jpg"><a href="vdo_top10.php" class="style24">10อันดับ</a></td>
      <td width="103" height="45" align="center" background="img/img09.jpg"><span class="style18 style23"><a href="evaluate_overview.php" class="style7">ประเมิน</a></span></td>
      <td width="103" height="45" align="center" background="img/img09.jpg"><span class="style7 style23">คู่มือการใช้</span></td>
      <td width="103" height="45" align="center" background="img/img09.jpg"><span class="style7 style23"><a href="admin_login.php" class="style7">ผู้ดูแลระบบ</a></span></td>
    </tr>
  </table>
</div>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td height="240">&nbsp;</td>
    <td width="830" valign="top" ><table width="100%" height="240" border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td height="170" align="center" valign="top" background="img/head_test3.jpg" class="style7">&nbsp;</td>
      </tr>
      <tr>
        <td align="center" valign="middle">&nbsp;</td>
      </tr>
    </table></td>
    <td height="240" align="right" valign="top" class="style16">
    <? if(isset($_SESSION["sess_Username"])){echo"<a href="."admin_logout.php"."><img src="."pic/logout(red)_icon.jpeg"." width='30' alt='ออกจากระบบ'/></a>";} ?>
    
    &nbsp;</td>
  </tr>
  <tr>
    <td width="210">&nbsp;</td>
    <td width="830" height="5">&nbsp;</td>
    <td align="center"></td>
  </tr>
  <tr>
    <td width="210" height="240" valign="top"><span class="style28"><a href="index.php" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('Home','','img/img09_2.jpg',1)"><a href="index.php" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Home','','img/img09_2.jpg',1)"></a></span>
    
      <span class="style27">
<?php 
	$keyword = $_GET["txtkeyword"];
	if($keyword != ''){
		$doc = new DOMDocument();
		$url='xml/KitchenGardenVegetables.xml';
		$doc->load($url);
		
		$items = $doc->getElementsByTagName($keyword)->item(0);
		if($items->nodeType == XML_ELEMENT_NODE){
				$count_syn_of =0;  
				$Target_syn_of = '';
			
				$count_is_a =0;  
				$Target_is_a = '';
			  
				$i=0;
				$Target = '';
				
				$test = $items->childNodes;
				
						foreach($test as $testloop){
						
								if($testloop->nodeType == XML_ELEMENT_NODE) {
							
			//							echo '<br>';
			//							echo 'nodeType ->'.$testloop->nodeType.'<br>';
			//							echo 'nodeName -> '.$testloop->nodeName.'<br>';
			//							echo 'nodeAttType -> '.$testloop->attributes->item(0)->nodeValue.'<br>';
			//							echo 'nodeValue -> '.$testloop->nodeValue.'<br>';
										if ($testloop->attributes->item(0)->nodeValue == ''){
										/*echo 'I Like This Node'.'<br>';*/
										$Target[$i] = $testloop->nodeName;
										/*echo $Target[$i].'<br>';*/
										$i++;
										}
										
										if ($testloop->attributes->item(0)->nodeValue == 'is-a'){
										$Target_is_a[$count_is_a] = $testloop->nodeValue;
										$count_is_a++;
										}
										
										if ($testloop->attributes->item(0)->nodeValue == 'syn-of'){
										$Target_syn_of[$count_syn_of] = $testloop->nodeValue;
										$count_syn_of++;
										}
							
								}
						
					  }
					  
						for($a=0;$a<$i;$a++){
						//echo 'Target['.$a.'] = ',$Target[$a],'<br>';
						}
						//echo '<br>';
					  //echo 'is-a = '.$count_is_a;
					  //echo '<br>';
					  
						for($i=0;$i<$count_is_a;$i++){
						//echo 'Target_is_a['.$i.'] = ',$Target_is_a[$i],'<br>';
						$keyword_is_a[$i] = $Target_is_a[$i];
						}
					 // echo 'syn-of = '.$count_syn_of;
					 // echo '<br>';
					  
						for($i=0;$i<$count_syn_of;$i++){
						//echo 'Target_syn_of['.$i.'] = ',$Target_syn_of[$i],'<br>';
						$keyword_syn_of[$i] = $Target_syn_of[$i];
						
							if(($count_syn_of - $i)!=1){ //========= ต่อ string sql command SYN-OF =========//
							$condition_syn_of = $condition_syn_of."vdo_name LIKE '%".$Target_syn_of[$i]."%' OR ";
							}else $condition_syn_of = $condition_syn_of."vdo_name LIKE '%".$Target_syn_of[$i]."%' ";
						}
				
			}
	}
	
	for($i=0;$i<$count_is_a;$i++){
	
		if(($count_is_a - $i)!=1){
		$condition_is_a1 = $condition_is_a1."vdo_name LIKE '%".$Target_is_a[$i]."%' OR ";
		}else $condition_is_a1 = $condition_is_a1."vdo_name LIKE '%".$Target_is_a[$i]."%' ";
			
	}
	
	for($j=0;$j<$count_syn_of;$j++){
			
		if(($count_syn_of - $j)!=1){ 
		$condition_is_a2 = $condition_is_a2."vdo_name NOT LIKE '%".$Target_syn_of[$j]."%' AND ";
		}else $condition_is_a2 = $condition_is_a2."vdo_name NOT LIKE '%".$Target_syn_of[$j]."%' AND vdo_name NOT LIKE '%".$_GET["txtkeyword"]."%' ";
			
	}
	
	//echo '<br> condition -> '.'('.$condition_syn_of.')'.'<br>';
	
	$SQL_SELECT[0] = "SELECT * FROM ag_vdodetail WHERE (vdo_name LIKE '%".$_GET["txtkeyword"]."%') order by vdo_id ;";
	//echo '<br>'.$SQL_SELECT[0].'<br>';
	
	$SQL_SELECT[1] = "SELECT * FROM ag_vdodetail WHERE ".'('.$condition_syn_of.') order by vdo_id';
	//echo '<br>'.$SQL_SELECT[1].'<br>';
	
	$SQL_SELECT[2] = "SELECT * FROM ag_vdodetail WHERE ".'('.$condition_is_a1.') AND ('.$condition_is_a2.') order by vdo_id';
	//echo '<br>'.$SQL_SELECT[2].'<br>';
	?>
      </span></td>
    <td width="830" align="center" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td height="20" colspan="3" background="img/tb_head.jpg">&nbsp;</td>
        </tr>
        <tr>
          <td width="20" background="img/tb_left.jpg">&nbsp;</td>
          <td width="95%" align="center"><table width="100%" border="0" cellspacing="0" cellpadding="0">
            
            <tr>
              <td height="30" align="left" bgcolor="#74FF22" class="style7"><span class="scapmain style4 style12 style25 style9">&nbsp;<img src="img/img05.gif" align="absbottom" /></span> สืบค้บขั้นสูง</td>
            </tr>
            <tr>
              <td align="center" bgcolor="#FFFFFF"><p>&nbsp;</p>
              
              
                  <table width="98%" height="247" border="0" cellpadding="0" cellspacing="0">
                    <tr>
                      <td align="center" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
                          <form action="<?=$_SERVER['SCRIPT_NAME'];?>" method="get" name="frmSearch" id="frmSearch" onSubmit="">
                            <tr>
                              <td width="74%" align="center" valign="middle" bgcolor="#74FF22">&nbsp;
                                <input name="txtkeyword" type="text" id="txtkeyword" size="50" value="<?=$_GET["txtkeyword"];?>" />
                              <input type="button" name="button2" id="button2" value="ล้าง"  onclick="txtreset()"/></td><td width="14%" align="left" valign="middle" nowrap="nowrap" bgcolor="#74FF22" class="style22">
<input name="cb_array[0]" type="checkbox" value="วีดีโอ" <? if($_GET["cb_array"][0] == "วีดีโอ") echo"checked"; ?> />
                                &nbsp;<span class="style25">วีดีโอ</span><br />
<input name="cb_array[1]" type="checkbox" value="เสียง"       <? if($_GET["cb_array"][1] == "เสียง") echo"checked"; ?> />
                              &nbsp;<span class="style25">เสียง</span> </td>
                              <td width="12%" align="center" valign="middle" bgcolor="#74FF22"><input type="submit" name="button" id="button" value="ค้นหา" /></td>
                            </tr>
                          </form>
                      </table>
                          <? 
include "connectdb.php" ;
$tb="ag_vdodetail"; 
$count_cbarray = count($_GET["cb_array"]);

////ถ้าไม่เป็นค่าว่าง////
if($_GET["txtkeyword"] != "") 
{
		/////////////////////////////////////// เก็บสถิติคำค้น (word) //////////////////////////////////////////
		if(isset($_SESSION["sess_userid"]) and !isset($_SESSION["sess_Username"])){
			$txtkeyword = $_GET["txtkeyword"];
			$user_id = $_SESSION["sess_userid"];
			
			$SQL_SELECT_WORD="SELECT * FROM ag_word WHERE word='$txtkeyword' ;";
			$db_query=mysql_db_query($dbname,$SQL_SELECT_WORD);
			$num_rows=mysql_num_rows($db_query);
			
			$word_id = $db_query[word_id];
			
			if($num_rows == 0){ //ถ้าไม่ซ้ำก็ insert
				$one = 1;
				$SQL_SELECT_MAXWORD = "SELECT MAX(word_id) FROM ag_word ;";
				$resultmax = mysql_query($SQL_SELECT_MAXWORD);
				$resultmax2 = mysql_result($resultmax,0);
				$resultmax3 = $resultmax2+1;
	
				echo"$resultmax2<br>";
				echo"$resultmax3<br>";
	
				$SQL_INSERT_WORD = "INSERT INTO ag_word VALUES('$resultmax3','$txtkeyword','1') ;";
				$result = mysql_db_query($dbname,$SQL_INSERT_WORD)or die("ไม่สามารถเพิ่มคำค้นได้ (word)");
				
				$SQL_INSERT_SEARCH = "INSERT INTO ag_search VALUES('$user_id','$resultmax3') ;";
				$result = mysql_db_query($dbname,$SQL_INSERT_SEARCH)or die("ไม่สามารถเพิ่มสถิติการค้นได้ได้ (search)");			
				
			}else if($num_rows == 1){ //ถ้ามีคำซ้ำก็ update
				
				$SQL_UPDATE_WORD = "UPDATE ag_word SET word_count=word_count+1 WHERE word='$txtkeyword' ;";
				$result = mysql_db_query($dbname,$SQL_UPDATE_WORD)or die("ไม่สามารถ update คำค้นได้ (word)");
				
				$SQL_SELECT_WORDID = "SELECT word_id FROM ag_word WHERE word='$txtkeyword' ;";
				$db_query=mysql_query($SQL_SELECT_WORDID);
				$word_id = mysql_result($db_query,0);
				
				$SQL_INSERT_SEARCH = "INSERT INTO ag_search VALUES('$user_id','$word_id') ;";
				$result = mysql_db_query($dbname,$SQL_INSERT_SEARCH)or die("ไม่สามารถเพิ่มสถิติการค้นได้ได้ (search)");	
			}
		
		}
		////////////////////////////////////////////////////////////////////////////////////////////////
//		if($_GET["txtfield"]=="1"){
		$condition = " WHERE (vdo_name LIKE '%".$_GET["txtkeyword"]."%') ";
		
				if($count_cbarray=='2'){
					$condition = $condition;
					}
				else if($count_cbarray=='1'){
					
						for ($i=0;$i<$count_cbarray;$i++) {
						$cb_array = $cb_array[$i];
					}
						
							if($_GET["cb_array"][0]=="วีดีโอ"){
							$condition = $condition." AND media_typeid = '1'";
							}else if($_GET["cb_array"][1]=="เสียง"){
							$condition = $condition." AND media_typeid = '2'";
							}
						
							}
		
//		}
//		else if($_GET["txtfield"]=="2"){
//		$condition = "WHERE (vdo_name LIKE '%".$_GET["txtkeyword"]."%' OR vdo_keyword LIKE '%".$_GET["txtkeyword"]."%') ";
//		//$condition = " WHERE (vdo_name LIKE '%".$_POST["txtkeyword"]."%') ";
//		
//	
//			if($count_cbarray=='2'){
//			$condition = $condition;
//			}
//			else if($count_cbarray=='1'){
//			
//				for ($i=0;$i<$count_cbarray;$i++) {
//				$cb_array = $cb_array[$i];
//				}
//				
//					if($_GET["cb_array"][0]=="วีดีโอ"){
//					$condition = $condition." AND media_typeid = '1'";
//					}else if($_GET["cb_array"][1]=="เสียง"){
//					$condition = $condition." AND media_typeid = '2'";
//					}
//				
//			}
//		}		
		
}
	////ถ้าเป็นค่าว่าง////
else if ($_GET["txtkeyword"] == "")
{
		if($count_cbarray=='2'){
		$condition = "";
		}else if($count_cbarray=='1'){
		
			for ($i=0;$i<$count_cbarray;$i++) {
			$cb_array = $cb_array[$i];
			}
			
				if($_GET["cb_array"][0]=="วีดีโอ"){
				$condition = "WHERE media_typeid = '1'";
				}else if($_GET["cb_array"][1]=="เสียง"){
				$condition = "WHERE media_typeid = '2'";
				}
			
		}
	
}
//$loop = 0;
for($loop=0;$loop<3;$loop++){
mysql_select_db($dbname) or die("เลือกฐานข้อมูลไม่ได้"); /* ทําการเลือกฐานขอมูลกอน */ 
$sql="SELECT * FROM $tb $condition order by vdo_id"; 
if($loop==1){$sql = $SQL_SELECT[1];
}elseif($loop==1){$sql = $SQL_SELECT[1];
}elseif($loop==2){$sql = $SQL_SELECT[2];
}
$db_query = "";
	//$sql = "select * from ag_vdodetail where vdo_id = 1";
	$db_query[$loop]=mysql_db_query($dbname,$sql); 
	if (!$db_query[$loop]){
	continue;
	}
	
//	if(($count_is_a == '0' && $loop == '1') or ($count_syn_of == '0' && $loop == '2')){
//	break;
//	
	$num_rows=mysql_num_rows($db_query[$loop]); /* นับ Reccord ทีพบ */
//		$loop =$loop+'1';
//		
//	}	
$numrow[$loop] = mysql_num_rows($db_query[$loop]);


$Per_Page = 10;   // จำนวนรายการสื่อมัลติมีเดียต่อ 1 หน้า
 
    $Page = $_GET["Page"];
    if(!$_GET["Page"])
    {
        $Page=1;
    }
 
    $Prev_Page = $Page-1;
    $Next_Page = $Page+1;
 
    $Page_Start = (($Per_Page*$Page)-$Per_Page);
    if($num_rows<=$Per_Page)
    {
        $Num_Pages =1;
    }
    else if(($num_rows % $Per_Page)==0)
    {
        $Num_Pages =($num_rows/$Per_Page) ;
    }
    else
    {
        $Num_Pages =($num_rows/$Per_Page)+1;
        $Num_Pages = (int)$Num_Pages;
    }
 
 
    $sql .=" ASC LIMIT $Page_Start , $Per_Page";
	$db_query[$loop]=mysql_db_query($dbname,$sql); 

$numrow_sum = $numrow[0]+$numrow[1]+$numrow[2];

if($numrow_sum=='0' && $loop=='2'){
	echo"<left><br><font color = black>ไม่พบข้อมูลที่ต้องการ<font></left>"; 
	break;
}else{
			if($loop==0){
						//echo"<left><br>จํานวน $num_rows รายการ</left>"; 

                          echo"<table width='100%' height='45' border='1' align='center' cellpadding='0' cellspacing='0' bordercolor='#999999' bgcolor='#E8AD35'>";
                          echo"<tr>";
                          echo"<td width='50' class='style19 style19'><div align='center' class='style7 style23'>หมวด</div></td>";
                          echo"<td width='' class='style19 style19'><div align='center' class='style24'>รายการ</div></td>";
                          echo"<td width='50' class='style19 style19'><div align='center' class='style24'>จำนวน<br />ผู้ชม</div></td>";
                          echo"<td width='87' class='style19 style19'><div align='center' class='style24'>เรทติ้ง</div></td>";
                          echo"<td width='50' class='style19 style19'><div align='center' class='style24'>จำนวน<br />ผู้โหวต</div></td>";
                          echo"</tr>";
                          echo"</table>";
						  
			}
 }                         
?>                          
                        <span class="style19 style19">
                          <?  ///////////////// วน loop แสดงข้อมูล //////////////////////

$a=0; 
$colorzero="#FFF2A4";
$colorone="#FEE86E";
$bgcolor="";
$vdo_icon = "wmv_icon.gif";
$sound_icon = "wma_icon.jpg";
$unknow_icon = "unknow_icon.jpg";
//while($a < $num_rows) 

while($result = mysql_fetch_array($db_query[$loop])) 
{ 
//$result = mysql_fetch_array($db_query); 
$vdo_id=$result[vdo_id]; 
$vdo_name=$result[vdo_name]; 
$vdo_desc=$result[vdo_desc]; 
$vdo_view=$result[vdo_view]; 
$mediatype=$result[media_typeid];

if($mediatype==1){
	$vdo_pic=$vdo_icon;
} else if($mediatype==2){
	$vdo_pic=$sound_icon;
}else $vdo_pic=$unknow_icon;
//$vdo_view=$result[vdo_name]; 
$_SESSION["sess_Url"]=$result[vdo_url]; 

//$SQL_SELECT_RATING="SELECT * FROM ag_vdodetail where vdo_id = $vdo_id";
//$db_query_rating=mysql_db_query($dbname,$SQL_SELECT_RATING); 
//$result_rating = mysql_fetch_array($db_query_rating); 
$votesum=$result[vdo_votesum]; 
$votecount=$result[vdo_votecount]; 

if($votesum==0 and $votecount==0){
$rating=0;
$votecount=0;
} else $rating = $votesum/$votecount;
////////// ปัดเศษทศนิยม ถ้าน้อยกว่า 0.5 ให้ปัดลง 0.5-0.75 ให้ปัดลงเศษ 0.5 ถ้า 0.75-1 ให้ปัดขึ้น //////////
		if(($rating-(int)$rating)>=0.5 and ($rating-(int)$rating)<0.75)
		{$rating++;
		$rating=(int)$rating-0.5;
		}
		else if(($rating-(int)$rating)>=0.75 and ($rating-(int)$rating)<1)
		{
		$rating++;
		$rating=(int)$rating;
		}
		else $rating=(int)$rating;
//////////////////////////////จบการปัดเศษ//////////////////////////////
	if (($a % 2) == 0) {
	$bgcolor=$colorzero;
	}
	else $bgcolor=$colorzero;
	
	?>
         </span>
         <table width="100%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#999999">
         <tr>
         <td width="50" bgcolor="<?=$bgcolor?>" class="style23"><!--<div align="center" class="style7"> <?echo"$vdo_id";?></div></td>    -->
         <div align="center" class="style7"> <img src='pic/<?=$vdo_pic?>' width='35'  border='0' /></div></td>
         <td width="" height="70" align="left" bgcolor="<?=$bgcolor?>" class="style19 style19"><br>
         <div align="left" class="style24"> &nbsp;&nbsp;<a href="vdo_player2.php?id=<?=$vdo_id?>" target="_self"><?echo"$vdo_name";?> </a>
         <img src="pic/new.gif"  /></div>
         <div align="left" class="style26"> &nbsp;&nbsp;&nbsp;&nbsp;<?echo"$vdo_desc";?></div><br>                       </td>
         <td width="50" bgcolor="<?=$bgcolor?>" class="style19 style19"><div align="center" class="style24"><?echo"$vdo_view";?></div></td>
         <td width="87" bgcolor="#000000" class="style19 style19"><div align="center" class="style24"><img src="pic/star_<?=$rating?>.gif" height="15" /></div></td>
         <td width="50" bgcolor="<?=$bgcolor?>" class="style19 style19"><div align="center" class="style24"><?echo"$votecount";?></div></td>
         </tr>
         </table>
         <span class="style19 style19">
	<? 
    $a++; 
} 
if($keyword==''){$loop=3;}

}///////จบ for loop
echo"<br>";
//echo"<center><br>จํานวน Reccord = $num_rows</center>"; 
//mysql_close(); 
?>
                          <!--Total <?php /*?><?= $num_rows;?><?php */?> Record :-->
                          <?php /*?><?=$Num_Pages;?><?php */?>
                          <!--Page :-->
                          <?
 	$cbarray0 = '&cb_array[0]=';
 	if($_GET["cb_array"][0] == "วีดีโอ") {
	$cbarray0 = '&cb_array[0]='.$_GET["cb_array"][0];
	}else $cbarray0 = '';
	
	$cbarray1 = '&cb_array[1]=';
	if($_GET["cb_array"][1] == "เสียง"){
	$cbarray1 = '&cb_array[1]='.$_GET["cb_array"][1];
	}else $cbarray1 = '';
				  
				  
				  
    if($Prev_Page)
    {
        echo " <b><a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page
		&txtfield=$_GET[txtfield]
		&txtkeyword=$_GET[txtkeyword]
		$cbarray1
		$cbarray0
		
		' ><font color = #006600><< ก่อนหน้า</font></a></b> "; //&cb_array[]=ภาพและเสียง&cb_array[]=เสียง&button=Search
    }
 
    for($i=1; $i<=$Num_Pages; $i++){
        if($i != $Page)
        {
            echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$i
			&txtfield=$_GET[txtfield]
			&txtkeyword=$_GET[txtkeyword]
			$cbarray1
			$cbarray0
			
			'><font color = #006600>$i</font></a> ";
        }
        else
        {
           if($num_rows != '0') echo "<b><font color = black>[ $i ]</font></b>";
        }
    }
    if($Page!=$Num_Pages)
    {
        echo " <b><a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page
		&txtfield=$_GET[txtfield]
		&txtkeyword=$_GET[txtkeyword]
		$cbarray1
		$cbarray0
			
		
		'><font color = '#006600'>ถัดไป>></font></a></b> ";
    }
     
    mysql_close();
 
     
    ?>
                        </span></td>
                    </tr>
                  </table>
                <p>&nbsp;</p></td>
            </tr>
            <tr>
              <td height="25" bgcolor="#74FF22">&nbsp;</td>
            </tr>
          </table></td>
          <td width="20" background="img/tb_right.jpg">&nbsp;</td>
        </tr>
        <tr>
          <td height="20" colspan="3" background="img/tb_foot.jpg">&nbsp;</td>
        </tr>
      </table>
      <p>&nbsp;</p>
      <table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td colspan="3"><img src="img/tb_head.jpg" width="100%" /></td>
        </tr>
        <tr>
          <td width="3%" height="30" align="left" bgcolor="#74FF22"><img src="img/tb_left.jpg" width="20" height="100%"  /></td>
          <td width="94%" height="25" align="center" valign="middle" bgcolor="#74FF22"><table width="100%" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td align="center"><span class="style27"><strong>&copy;2009 All Rights Reserved. &nbsp;&bull;&nbsp; ออกแบบโดย นิสิตมหาวิทยาเกษตรศาสตร์ วิทยาเขตศรีราชา</strong></span></td>
            </tr>
          </table>            </td>
          <td width="3%" height="30" align="right" bgcolor="#74FF22"><img src="img/tb_right.jpg" width="20" height="100%"  /></td>
        </tr>
        <tr>
          <td colspan="3"><img src="img/tb_foot.jpg" width="100%" /></td>
        </tr>
      </table>    </td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td colspan="3">&nbsp;</td>
  </tr>
</table>
</body>
</html>
